package com.fengmang.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;


/**
 * @author: fuchenming
 * @create: 2019-08-09 09:28
 */
public class ProxyActivity extends Activity {
    private Class<?> localClass;

    private Object localInstance;

    Context context;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String activityName = getIntent().getStringExtra("className");
//
//        Log.e("p_pa===onCreate", "==="+className);

//        loadAPK( "/data/data/com.fengmang.myapplication/new.apk", "/data/data/com.fengmang.myapplication/", activityName);    //Y
//        loadAPK( Environment.getExternalStorageDirectory() + File.separator + "new.apk", "/data/data/com.fengmang.myapplication/", activityName);    //Y
        loadAPK( Environment.getExternalStorageDirectory() + File.separator + "new.apk", Environment.getExternalStorageDirectory() + File.separator, activityName);    //Y

    }

    @Override
    public Resources getResources() {
         return PluginManager.getInstance().getResources()==null?super.getResources():PluginManager.getInstance().getResources();
    }

    private void loadAPK(String dexPath, String dexOutputPath, String activityName) {

        try {

            ClassLoader localClassLoader = ClassLoader.getSystemClassLoader();
            DexClassLoader localDexClassLoader = new DexClassLoader(dexPath, dexOutputPath, null, localClassLoader);

            PackageInfo localPackageInfo = getPackageManager().getPackageArchiveInfo(dexPath, PackageManager.GET_ACTIVITIES);

            Log.e("loadAPK===0", "==="+localPackageInfo);

//            if(localPackageInfo==null){
//////                localClass = this.loadClass(activityName);
////                localClass = Class.forName(activityName);
//////                Constructor<?> localConstructor = localClass.getConstructor();
////                localInstance = localClass.newInstance();
////
////                Log.e("loadAPK>>>1", localClass+"==="+localInstance);
////
////
////
////                Method onCreateMethod = localClass.getDeclaredMethod("onCreate", new Class[] {Bundle.class});
////                onCreateMethod.setAccessible(true);
////                onCreateMethod.invoke(localInstance, new Object[] {null});
//
//
//                setContentView(R.layout.activity_main4);
//
//                Log.e("tra===2", "==="+this);
//
//                Toast.makeText(this, "xxx"+new B().test(), Toast.LENGTH_SHORT).show();
//
//                Log.e("loadAPK>>>2", localClass+"==="+localInstance);
//            }else
            if ((localPackageInfo.activities != null) && (localPackageInfo.activities.length > 0)) {

//                String activityName = localPackageInfo.activities[0].name;
//                String activityName = "com.fengmang.myapplication.TestRoomAActivity";

                localClass = localDexClassLoader.loadClass(activityName);
                Constructor<?> localConstructor = localClass.getConstructor(new Class[] {Context.class});
                localInstance = localConstructor.newInstance(new Object[] {this});

                Log.e("loadAPK===1", localClass+"==="+localInstance);



//                AssetManager manager = AssetManager.class.newInstance();
//                Method addAssetPath = AssetManager.class.getMethod("addAssetPath", String.class);
//                addAssetPath.invoke(manager, dexPath);
//                resources = new Resources(manager, getResources().getDisplayMetrics(), getResources().getConfiguration());

                Log.e("loadAPK===2", localClass+"==="+localInstance);

//                Method getResourcesMethod = localClass.getDeclaredMethod("getResources", null);
//                getResourcesMethod.setAccessible(true);
//                getResourcesMethod.invoke(localInstance, null);
//
//                Log.e("loadAPK===2", localClass+"==="+localInstance);

//                Class<?> localClass2 = localDexClassLoader.loadClass("com.fengmang.myapplication.RoundImageView");
//                Constructor<?> localConstructor2 = localClass2.getConstructor(new Class[] {Context.class});
//                localConstructor2.newInstance(new Object[] {this});
//
//                Log.e("loadAPK===3", localClass+"==="+localInstance);
//
//                int obj2 = getResources().getIdentifier("attrs", "values", getPackageName());
//                LayoutInflater.from(this).inflate(obj2, null);

//                Class<?> layout = localDexClassLoader.loadClass("com.fengmang.myapplication.R$layout");
//                Field field = layout.getField("activity_main2");
//                Integer obj = (Integer) field.get(null);
                int obj = getResources().getIdentifier("activity_main2", "layout", getPackageName());
                View view = LayoutInflater.from(this).inflate(getResources().getLayout(obj), null);
                // 或者这样，但一定要重写getResources方法，才能这样写

//                View view = LayoutInflater.from(this).inflate(obj, null);

                Log.e("loadAPK===4-0", localClass+"==="+localInstance);

                Method method = localClass.getDeclaredMethod("setLayout", View.class);
                method.setAccessible(true);
                Log.e("loadAPK===4-1", localClass+"==="+localInstance);
                method.invoke(localInstance, view);

                Log.e("loadAPK===4-2", localClass+"==="+localInstance);


                Method onCreateMethod = localClass.getDeclaredMethod("onCreate", new Class[] {Bundle.class});
                onCreateMethod.setAccessible(true);
                onCreateMethod.invoke(localInstance, new Object[] {null});

                Log.e("loadAPK===5", localClass+"==="+localInstance);
            }

        } catch (Exception e) {
            Log.e("loadAPK===e", "==="+e);
            e.printStackTrace();
        }

    }

//    @Override
//    public void startActivity(Intent intent) {
//        String className = intent.getStringExtra("className");
//        Intent intent1 = new Intent(this, com.fengmang.scouringpad.ProxyActivity.class);
//        intent1.putExtra("className", className);
//        super.startActivity(intent1);
//    }
//
//    //重写加载类
//    @Override
//    public ClassLoader getClassLoader() {
//        Log.e("p_getClassLoader===", super.getClassLoader()+"==="+PluginManager.getInstance().getDexClassLoader());
//        return PluginManager.getInstance().getDexClassLoader()==null?super.getClassLoader():PluginManager.getInstance().getDexClassLoader();
//    }
//
//    //重写加载资源
//    @Override
//    public Resources getResources() {
//        Log.e("p_getResources===", super.getResources()+"==="+PluginManager.getInstance());
//        return PluginManager.getInstance().getResources()==null?super.getResources():PluginManager.getInstance().getResources();
//    }

//    @Override
//    protected void onStart() {
//        super.onStart();
//        payInterfaceActivity.onStart();
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        payInterfaceActivity.onResume();
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        payInterfaceActivity.onStop();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        payInterfaceActivity.onDestroy();
//    }
}